package com.by_man.presenter.module_health_records.access_records;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.presenter.module_health_consultant.BitmapCache;
import com.by_man.repository.IpAddress;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by man on 2017-03-18.
 */
public class PhysicalExaminationListActivity extends AppCompatActivity {
    private RequestQueue rQueue;
    private ImageLoader imageLoader;
    private ArrayList<String> listItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        //rQueue = Volley.newRequestQueue(this);
        //imageLoader = new ImageLoader(rQueue, new BitmapCache());
        imageLoader = SharedApplication.getImageLoader(getApplication());
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, PhysicalExaminationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, PhysicalExaminationActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", 0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        GridView lv = (GridView) findViewById(R.id.gridView);
        ArrayList<Map<String, Object>> json = Images.imageTijian;
        for(int j=0;j<json.size();j++){
            Map<String, Object> test=json.get(j);
            final String imgURL="http://" + IpAddress.ServerIP + "/"+test.get("path");
            listItem.add(imgURL);
        }

        lv.setAdapter(new SimpleAdapter());
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String url = listItem.get(position);
                Intent intent = new Intent(PhysicalExaminationListActivity.this,PhotoCheckerActivity.class);
                intent.putExtra("bitmap", url);
                intent.putExtra("extra_activity",PhysicalExaminationListActivity.this.getClass().getName());
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }

    private class SimpleAdapter extends BaseAdapter {
        @Override
        public int getCount()
        {
            return listItem.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listItem.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
            if(null == convertView) {
                convertView = View.inflate(PhysicalExaminationListActivity.this, R.layout.item_gridview, null);
            }
            ImageView imageview = (ImageView)convertView.findViewById(R.id.imageView);
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageview,R.drawable.plugin_camera_no_pictures,
                    R.drawable.plugin_camera_no_pictures);
            imageLoader.get(listItem.get(position),listener);
            return convertView;
        }
    }
}
